import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { ItemData } from "../resource/info/ItemData";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { GameProxy } from "../mvc/GameProxy";
import { GuideAutoType } from "../rules/GuideAutoType";
import { IGuideItemModule } from "../../../modules/interfaces/IGuideItemModule";
import { EDressPos } from "../../../Message/Public/EDressPos";
import { EDrug } from "../../../Message/Public/EDrug";
import { FlyToNavbarTool } from "../view/award/FlyToNavbarTool";
import { SkillInfo } from "../model/SkillInfo";
import { SkillsUtil } from "../scene/skill/SkillsUtil";
import { WindowEvent } from "../../component/window/WindowEvent";
import { IView } from "../../mvc/interfaces/IView";
import { GuideItemModule } from "../../../modules/GuideItemModule";
import { LayerManager } from "../manager/LayerManager";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
//class GuideItemController
    
    export  class GuideItemController extends Controller
    {
       

        protected onGuideItemRes(arg1: DataEvent): void
        {
            var loc2=0;
            var loc1=arg1.data as ItemData;
            if (loc1 != null) 
            {
                if (ItemsUtil.isPackage(loc1)) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, loc1));
                }
                else if (loc1.itemInfo.item.category != ECategory._ECategoryDrug) 
                {
                    if (loc1.category != ECategory._ECategoryPet) 
                    {
                        if (ItemsUtil.isPetEgg(loc1)) 
                        {
                            cache.guide.guidePetAdd = true;
                            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_UsePetEgg, loc1));
                        }
                        else if (loc1.category != ECategory._ECategoryMount) 
                        {
                            if (loc1.itemInfo.item.category == ECategory._ECategoryProp && loc1.itemInfo.item.type == EProp._EPropSkillBook) 
                            {
                                if (!cache.guide.guideSkillLearn) 
                                {
                                    cache.guide.skillLearnAlert = true;
                                    GameProxy.skillProxy.learnByBook(loc1);
                                }
                            }
                            else if (cache.guide.autoType != GuideAutoType.FullAuto) 
                            {
                                if (ItemsUtil.isCanGuideUseInBag(loc1)) 
                                {
                                    view.show();
                                    (view as IGuideItemModule).updateData(loc1);
                                    if (loc1.category == ECategory._ECategoryProp && loc1.type == EProp._EPropSkillBook) 
                                    {
                                        cache.guide.skillLearnAlert = true;
                                    }
                                }
                            }
                            else 
                            {
                                if (loc1.itemInfo.item.level >= 10 && loc1.itemInfo.item.category == ECategory._ECategoryEquip) 
                                {
                                    if (!ItemsUtil.isMoveGoodEquip(loc1)) 
                                    {
                                        if (cache.guide.guideItemAlert) 
                                        {
                                            cache.guide.guideItemAlert = false;
                                            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_ItemUsed));
                                        }
                                        return;
                                    }
                                }
                                Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, loc1));
                            }
                        }
                        else 
                        {
                            cache.guide.guideMount = loc1;
                            if (loc1.posType == 0) 
                            {
                                loc2 = cache.pack.backPackCache.getIndexByUid(loc1.uid);
                            }
                            loc2 = cache.pack.getPackChacheByPosType(loc1.posType).getIndexByUid(loc1.uid);
                            GameProxy.roleProxy.dress_async(loc1, EDressPos._EDressPosMount, loc1.posType, loc2);
                            Dispatcher.addEventListener(EventName.BackPackReqData, this.onPackReqDataRes);
                        }
                    }
                    else 
                    {
                        cache.guide.petGuide = true;
                    }
                }
                else if (!(loc1.type == EDrug._EDrugPetEnergy || loc1.type == EDrug._EDrugPetLifeSpan)) 
                {
                    if (ItemsUtil.isChangeShape(loc1)) 
                    {
                        if (cache.guide.autoType != GuideAutoType.FullAuto) 
                        {
                            view.show();
                            (view as IGuideItemModule).updateData(loc1);
                        }
                        else 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, loc1));
                        }
                    }
                    else 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.AddToShortcutsByReq, loc1));
                    }
                }
            }
            if (ItemsUtil.isCanGuideUseInBag(loc1)) 
            {
                if (this._lastItem != null) 
                {
                    this.onItemUseReq(null);
                }
                this._lastItem = (view as IGuideItemModule).item;
            }
            return;
        }

        /* internal  */onPackReqDataRes(arg1: DataEvent): void
        {
            if (!(cache.guide.guideMount == null) && cache.guide.guideMount.uid == String(arg1.data)) 
            {
                Dispatcher.removeEventListener(EventName.BackPackReqData, this.onPackReqDataRes);
                Dispatcher.dispatchEvent(new DataEvent(EventName.CallMounts, cache.guide.guideMount));
                cache.guide.guideMount = null;
            }
            return;
        }

        /* internal  */onUseLevelAwardThing(arg1: DataEvent): void
        {
            this.useItem(arg1.data as ItemData);
            return;
        }

        /* internal  */onItemUseReq(arg1: DataEvent): void
        {
            var loc1=null;
            if (arg1 != null) 
            {
                loc1 = arg1.data as ItemData;
            }
            else 
            {
                loc1 = this._lastItem;
            }
            if (loc1 == null) 
            {
                return;
            }
            this.useItem(loc1);
            this._lastItem = null;
            return;
        }

        /* internal  */useItem(arg1: ItemData): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=false;
            if (arg1.category != ECategory._ECategoryMount) 
            {
                loc1 = arg1.uid;
                loc2 = cache.pack.backPackCache.getIndexByUid(loc1);
                if (loc2 != -1) 
                {
                    if (arg1.itemInfo.item.category == ECategory._ECategoryProp && arg1.itemInfo.item.type == EProp._EPropSkillBook) 
                    {
                        GameProxy.skillProxy.learnByBook(arg1);
                    }
                    else if (!(arg1.itemInfo.item.category == ECategory._ECategoryDrug && !ItemsUtil.isChangeShape(arg1))) 
                    {
                        if (arg1.itemInfo.item.category == ECategory._ECategoryProp && arg1.itemInfo.item.type == EProp._EPropVIPCard) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_ShowVipCard, {"bmp":(view as IGuideItemModule).itemBorder.bitmapdata, "start":(view as IGuideItemModule).globalPoint()}));
                            Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, arg1));
                        }
                        else 
                        {
                            loc3 = true;
                            if (arg1.itemInfo.item.category != ECategory._ECategoryEquip) 
                            {
                                FlyToNavbarTool.flyToBackPack((view as IGuideItemModule).itemBorder.bitmapdata, (view as IGuideItemModule).globalPoint());
                            }
                            else 
                            {
                                if (arg1.itemInfo.item.level >= 10 && arg1.itemInfo.item.category == ECategory._ECategoryEquip) 
                                {
                                    if (!ItemsUtil.isMoveGoodEquip(arg1)) 
                                    {
                                        loc3 = false;
                                    }
                                }
                                if (loc3) 
                                {
                                    FlyToNavbarTool.flyToPalyer((view as IGuideItemModule).itemBorder.bitmapdata, (view as IGuideItemModule).globalPoint());
                                }
                                else 
                                {
                                    FlyToNavbarTool.flyToBackPack((view as IGuideItemModule).itemBorder.bitmapdata, (view as IGuideItemModule).globalPoint());
                                }
                            }
                            if (loc3 && ItemsUtil.isCanGuideUseInBag(arg1)) 
                            {
                                Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, arg1));
                            }
                        }
                    }
                }
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CallMounts, arg1));
            }
            return;
        }

        /* internal  */onSkillLearnGuideRes(arg1: SkillInfo): void
        {
            if (SkillsUtil.instance.isAutoAddToShortCart(arg1.tSkill)) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AddToShortcutsByReq, arg1));
            }
            else if (SkillsUtil.instance.isAutoAddToShortCart2(arg1.tSkill)) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AddToBuffSkillByReq, arg1));
            }
            return;
        }

        /* internal  */onBackPackUse(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            if (this._lastItem && this._lastItem.uid == loc1) 
            {
                this._lastItem = null;
                view.hide();
            }
            return;
        }

        /* internal  */onMountsCallRes(arg1: ItemData): void
        {
            if (this._lastItem && this._lastItem.uid == arg1.uid) 
            {
                this._lastItem = null;
                view.hide();
            }
            return;
        }

        /* internal  */onSkillLearnRes(arg1: SkillInfo): void
        {
            if (!arg1) 
            {
                return;
            }
            if (this._lastItem && this._lastItem.itemInfo.item.category == ECategory._ECategoryProp && this._lastItem.itemInfo.item.type == EProp._EPropSkillBook) 
            {
                if (this._lastItem.itemInfo.item.effect == arg1.tSkill.skillId) 
                {
                    this._lastItem = null;
                    view.hide();
                }
            }
            return;
        }

        /* internal  */onItemModuleClose(arg1: WindowEvent): void
        {
            if (cache.guide.guideItemAlert) 
            {
                cache.guide.guideItemAlert = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_ItemUsed));
            }
            if (this._lastItem && this._lastItem.itemInfo.item.category == ECategory._ECategoryProp && this._lastItem.itemInfo.item.type == EProp._EPropVIPCard) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_ShowVipCard, {"bmp":(view as IGuideItemModule).itemBorder.bitmapdata, "start":(view as IGuideItemModule).globalPoint()}));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, this._lastItem));
                this._lastItem = null;
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            var loc1=new GuideItemModule();
            loc1.addEventListener(EventName.Guide_ItemUse, this.onItemUseReq);
            loc1.addEventListener(WindowEvent.CLOSE, this.onItemModuleClose);
            loc1.layer = LayerManager.popupLayer;
            return loc1;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.GetTaskItemAndGuide, this.onGuideItemRes);
            Dispatcher.addEventListener(EventName.BackPackReqData, this.onBackPackUse);
            Dispatcher.addEventListener(EventName.UseLevelAwardThing, this.onUseLevelAwardThing);
            NetDispatcher.addCmdListener(ServerCommand.SkillLearnGuideRes, this.onSkillLearnGuideRes);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpdate, this.onSkillLearnRes);
            return;
        }

        private /* var */_lastItem: ItemData;
    }
